<?php
namespace app\admin\controller;
use think\Db;
class Slidecat extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = request()->post();
            $map = [];
            if($d['keyword']!=''){
                $map[] = ['cat_title','like',"%{$d['keyword']}%"];
            }
        	$rs = model('SlideCat')::where($map)
                                ->withCount('Slide')
                                ->order('sort desc, id desc')
                                ->paginate($d['limit'],false,['page'=>$d['page']]);
            if($rs){
                $rs = $rs->toArray();
                return suc($rs['data'], $rs['total']);
            }else{
                return suc([],0);
            }
        }
        $catlist = model('SlideCat')->order('sort desc, id desc')->column('cat_title','id');
        return $this->fetch('',['catlist'=>$catlist]);
    }

    //添加
    public function add(){
        if(is_post()){
            $d = request()->post();
            if(model('SlideCat')->renew($d)){
                session('js.function','preload');
                return suc();
            }else{
                return err(model('SlideCat')->getError());
            }
        }
        $catlist = model('SlideCat')->order('sort desc, id desc')->column('cat_title','id');
        return $this->fetch('',['catlist'=>$catlist]);
    }

    //编辑
    public function edit($id=''){
        if(is_post()){
            $d = request()->post();
            if(model('SlideCat')->renew($d)){
                session('js.function','preload');
                return suc();
            }else{
                return err(model('SlideCat')->getError());
            }
        }
        $catlist = model('SlideCat')->order('sort desc, id desc')->column('cat_title','id');
        $info = model('SlideCat')::get($id);
        return $this->fetch('',['info'=>$info,'catlist'=>$catlist]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = input();
            $rs = model('SlideCat')->del($d['id']);
            session('js.function','reload');
            if($rs){
                return suc();
            }else{
                return err(model('SlideCat')->getError());
            }
        }
    }
    //详情
    public function detail($id){
        return $this->fetch();
    }
}
